#include "dominion_helpers.h"
#include "dominion.h"
#include "rngs.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int randomGameState(int players, struct gameState *g)
{
	SelectStream(2);
	PutSeed(3);
	int i, j, supply, turn, hand, deck;

	supply = floor(Random() * (treasure_map + 1));
	turn = floor(Random() * players);
	hand = floor(Random() * MAX_HAND);
	deck = floor(Random() * MAX_DECK);

	g->numPlayers = players;
	for (i = 0; i < supply; i++) {
		g->supplyCount[i] = floor(Random() * 10);	
		g->embargoTokens[i] = floor(Random() * 5);
	}
	g->outpostPlayed = floor(Random() * 20);
	g->outpostTurn = turn; 
	g->whoseTurn = turn;
	g->phase = floor(Random() * 3);
	g->coins = floor(Random() * 10);
	g->numActions = floor(Random() * 5);
	g->numBuys = floor(Random() * 5);
	g->playedCardCount = 0;
	for (i = 0; i < deck; i++) {
		g->playedCards[i] = floor(Random() * 27);
	}
	for (i = 0; i < players; i++) {
		g->handCount[i] = 0;
		g->deckCount[i] = 0;
		g->discardCount[i] = 0;
		for (j = 0; j < hand; j++) {
			g->hand[i][j] = floor(Random() * 27);
			g->handCount[i]++;
		}
		for (j = 0; j < deck; j++) {
			g->deck[i][j] = floor(Random() * 27);
			g->deckCount[i]++;
			g->discard[i][j] = floor(Random() * 27);
			g->discardCount[i]++;
		}
	}
	return 0;
}

int checkGainCard(int player, struct gameState *current)
{
	struct gameState test;
	memcpy(&test, current, sizeof(struct gameState));
	
	int card, pile;
	card = floor(Random() * (treasure_map + 1));
	pile = floor(Random() * 3); // 0 = discard 1 = deck 2 = hand

	gainCard(card, current, pile, player);

	if (pile == 0) {
		test.discard[player][test.discardCount[player]] = card;
		test.discardCount[player]++;
	} else if (pile == 1) {
		test.deck[player][test.deckCount[player]] = card;
		test.deckCount[player]++;
	} else if (pile == 2) {
		test.hand[player][test.handCount[player]] = card;
		test.handCount[player]++;
	}
	
	test.supplyCount[card]--;

	if (memcmp(&test, current, sizeof(struct gameState)) != 0)
	{
		printf("------- card: %d pile: %d\n", card, pile);
		printf("test> discard: %d\n", test.discardCount[player]); //discardCount: %d deck: %d deckCount: %d\n test> hand: %d handcount: %d\n",
				//test.discard[player][test.discardCount[player]], test.discardCount[player],
				//test.deck[player][test.deckCount[player]], test.deckCount[player],
				//test.hand[player][test.handCount[player]], test.handCount[player]);
	}

	return 0;
}

int main()
{
	printf("Testing gainCard:\n");

	int i, p;
	struct gameState G;
	
	for (i = 0; i < 20; i++) {
		p = floor(Random() * MAX_PLAYERS);
		randomGameState(p, &G);
		checkGainCard(p, &G);
	}
	
	printf("Tests Done\n");

	return 0;
}
